﻿using KeZhou.ViewModels;
using System.Text.RegularExpressions;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;


namespace KeZhou.Views.Components
{
    /// <summary>
    /// SignUpControl.xaml 的交互逻辑
    /// </summary>
    public partial class SignUpControl : UserControl
    {
        //改变控件所需要的参数
        public delegate void SwitchControlEventHandler(object sender, ControlType controlType);
        public event SwitchControlEventHandler SwitchControl;
        //60s发送一次验证码需要的计时器
        Timer timer = new Timer(1000);
        int sec = 0;

        public SignUpControl()
        {
            InitializeComponent();
            //绑定数据
            this.DataContext = SignUpViewModel.Instance;
            //绑定计时器
            timer.Elapsed += TimerElapsed;
        }

        /*
         * 切换控件
         */
        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SwitchControl?.Invoke(this, ControlType.Login);
        }

        /*
         * 按钮事件-获取验证码，在View层初步验证手机格式
         */
        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Application.Current.MainWindow is MainWindow mainWindow)
            {
                if (Regex.IsMatch(suPhone.Text, @"^1[3456789]\d{9}$"))
                {
                    if (PhoneCodeViewModel.Instance.getCodeByPhone(suPhone.Text,0))
                    {
                        sixtySec();
                    }
                    else
                    {
                        //后端发送验证码失效
                        mainWindow.showWrongMsg(1);
                    }
                }
                else
                {
                    //手机号格式错误
                    mainWindow.showWrongMsg(0);
                }
            }
        }

        //60s倒计时
        private void sixtySec()
        {
            sec = 60;
            timer.Start();
            Dispatcher.Invoke(() => getCodeTB.IsHitTestVisible = false);
        }

        //timer开始时每秒钟触发
        private void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            if (sec > 0)
            {
                Dispatcher.Invoke(() => getCodeTB.Text = sec + "秒后可重发");
                sec -= 1;
            }
            else
            {
                timer.Stop();
                Dispatcher.Invoke(() => {
                    getCodeTB.Text = "点击获取短信验证码";
                    getCodeTB.IsHitTestVisible = true;
                });
            }
        }

        /*
         * 按钮事件-注册账号，在View层初步验证格式和是否为空
         */
        private void Border_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            if (Application.Current.MainWindow is MainWindow mainWindow)
            {
                if (!(string.IsNullOrEmpty(suPhone.Text) || string.IsNullOrEmpty(suPassword1.Text) || string.IsNullOrEmpty(suPassword2.Text) || string.IsNullOrEmpty(suCode.Text)))
                {
                    if (suPassword1.Text.Equals(suPassword2.Text))
                    {
                        mainWindow.showWrongMsg(SignUpViewModel.Instance.signUpAccount());
                    }
                    else
                    {
                        //两次输入的密码不一致
                        mainWindow.showWrongMsg(3);
                    }
                }
                else
                {
                    //没有输入完整的账号密码和验证码
                    mainWindow.showWrongMsg(2);
                }
            }
        }
    }
}
